package com.achun.ximalayamvvm.view.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.achun.ximalayamvvm.R
import com.achun.ximalayamvvm.databinding.RecommendListItemBinding
import com.bumptech.glide.Glide
import com.ximalaya.ting.android.opensdk.model.album.Album
import java.util.ArrayList

class RecommendListAdapter: RecyclerView.Adapter<RecommendListAdapter.InnerHolder>() {
    private var albums=ArrayList<Album>()
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): RecommendListAdapter.InnerHolder {
        var view =
            LayoutInflater.from(parent.context).inflate(R.layout.recommend_list_item, parent, false)
        var inflate = DataBindingUtil.inflate<RecommendListItemBinding>(
            LayoutInflater.from(parent.context),
            R.layout.recommend_list_item,
            parent,
            false
        )

        return InnerHolder(inflate)
    }

    override fun onBindViewHolder(holder: RecommendListAdapter.InnerHolder, position: Int) {
        holder.binding1.also {bind->
            with(albums[position]){
               bind.recommendItemTitle.text=albumTitle
                bind.recommendItemContent.text=albumIntro.trim()
                bind.recommendItemPlaycount.text=(this.playCount/10000).toString()+"万"
                bind.recommendItemAllgather.text=includeTrackCount.toString()+"集"
                Glide.with(holder.itemView.context).load(this.coverUrlSmall).into(bind.recommendItemCover)
                //点击事件
                bind.root.setOnClickListener{
                    onClickItemListenter.onclick(this)
                }
                bind.root.setOnLongClickListener{
                    onLongClickItemListenter.onclick(this)
                    true
                }
            }
        }


    }

    override fun getItemCount(): Int {
        return if (albums==null) 0 else albums.size
    }

    class InnerHolder(binding:RecommendListItemBinding):
        RecyclerView.ViewHolder(binding.root){
           var binding1:RecommendListItemBinding = binding


    }
    fun setDatas(albums:List<Album>){
        this.albums= albums as ArrayList<Album>
        notifyDataSetChanged()
    }
    private lateinit var onClickItemListenter:OnClickItemListenter
    interface  OnClickItemListenter{
        fun onclick(album:Album)
    }
    fun setOnclickItemListenter(onClickItemListenter: OnClickItemListenter){
        this.onClickItemListenter=onClickItemListenter
    }
    private lateinit var onLongClickItemListenter: OnLongClickItemListenter
    interface OnLongClickItemListenter{
        fun onclick(album: Album)
    }
    fun setOnLongClickItemListenter(onLongClickItemListenter: OnLongClickItemListenter){
        this.onLongClickItemListenter=onLongClickItemListenter
    }

}